﻿using System;
using System.IO.IsolatedStorage;

namespace Teele
{
    public class AppSettings
    {
        IsolatedStorageSettings settings;

        public AppSettings()
        {
            if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
            {
                settings = IsolatedStorageSettings.ApplicationSettings;
            }
        }

        public bool AddOrUpdateValue(string key, Object value)
        {
            bool valueChanged = false;

            if (settings.Contains(key))
            {
                if (settings[key] != value)
                {
                    settings[key] = value;
                    valueChanged = true;
                }
            }
            else
            {
                settings.Add(key, value);
                valueChanged = true;
            }

            return valueChanged;
        }

        public T GetValueOrDefault<T>(string key, T defaultValue)
        {
            T value;

            if (settings.Contains(key))
            {
                value = (T)settings[key];
            }
            else
            {
                value = defaultValue;
            }

            return value;
        }

        public void save()
        {
            settings.Save();
        }

        public int SpeedCamAlertDistance
        {
            get
            {
                return GetValueOrDefault<int>("SpeedCamAlertDistance", 500);
            }
            set
            {
                if (AddOrUpdateValue("SpeedCamAlertDistance", value))
                {
                    save();
                }
            }
        }

        public bool DisableAutomaticIdle
        {
            get
            {
                return GetValueOrDefault<bool>("DisableAutomaticIdle", true);
            }
            set
            {
                if (AddOrUpdateValue("DisableAutomaticIdle", value))
                {
                    save();
                }
            }
        }
    }

    public class AlertDistance
    {
        public string Description { get; set; }
        public int Distance { get; set; }
    }
}
